import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutterdemo/models/entity/banner_entity.dart';
import 'package:flutterdemo/models/entity/base_entity.dart';
import 'package:flutterdemo/models/entity/user_entity.dart';
import 'package:flutterdemo/utils/sp_util.dart';
import 'package:flutterdemo/utils/toast_util.dart';
import 'package:http/http.dart' as http;
import 'http.dart';

///io
///

var ip = "https://www.wanandroid.com/";
var map = Map<String, String>();

class DartIo {
//  var ip = "https://www.wanandroid.com/";

  get() async {
    var httpClient = new HttpClient();
    HttpClientRequest request =
        await httpClient.getUrl(Uri.parse("${ip}wxarticle/chapters/json"));
    HttpClientResponse response = await request.close();
    if (response.statusCode == HttpStatus.ok) {
      var responseBody = await response.transform(utf8.decoder).join();
      var json = jsonDecode(responseBody);
      print("responseBody:$responseBody");
      print("json:$json");
    } else {
      print("response.statusCode=${response.statusCode}");
    }
  }
}

class HttpTest {
  get() async {
    http.Client()
        .get("${ip}wxarticle/chapters/json")
        .then((http.Response response) {
      print("http responseBod:${response.body}");
    });
  }

  post() async {
    map["username"] = "江渚清沨";
    map["password"] = "jerry1990liu";
    http.Client()
        .post("${ip}user/login", body: map)
        .then((http.Response response) {
      print("http responseBody:${response.body}");
    });
  }
}

class DioTest {
  DioTest();

  get() async {
    var response = await Dio().get("${ip}banner/json");
    //    print(
//        "dio statusCode:${response.statusCode},data:${response.data.toString()}");
    HttpManager().get("banner/json", null, (data) {
      BaseEntity<List<BannerEntity>> baseEntity = BaseEntity.fromJson(data);
      baseEntity.data.forEach((element) {
        print(element.imagePath);
      });
    }, (error) {
      ToastUtil.showToastCenter(error);
    });
  }

  post() async {
//    var response=await Dio().post("${ip}user/login",data: {"username":"江渚清沨","password":"jerry1990liu"});
    //表单
    var map = Map<String, String>();
    map["username"] = "江渚清沨";
    map["password"] = "jerry1990liu";
    FormData formData = FormData.fromMap(map);
    HttpManager().post("user/login", formData, (data) {
      BaseEntity<UserEntity> baseModel = BaseEntity.fromJson(data);
      SpUtil().putString("username", baseModel.data.username);
      print("SpUtil=${SpUtil().toString()}");
      print("SpUtil=${SpUtil().toString()}");
      print("解析${baseModel.errorMsg}");
    }, (error) {
      ToastUtil.showToastCenter(error);
    });
  }
}
